package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQLDTTXAChuongTrinhHocChiTiet;
import com.lb.jwitter.service.TblQLDTTXAChuongTrinhHocChiTietService;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHoc;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHocService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocDuKienService;
import com.lb.jwitter.service.TblQLDTTXAMonHoc;
import com.lb.jwitter.service.TblQLDTTXAMonHocService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAGiaoVien;
import com.lb.jwitter.service.TblQLDTTXAGiaoVienService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocKyTrongNamtblQLDTTXALopMonHocDuKienAutoInsertAction{
    private Message msg; 
    public Message getMsg() { return msg; }

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private TblQLDTTXADMKhoaHocService tblQLDTTXADMKhoaHocService;
    private TblQLDTTXAChuongTrinhHocChiTietService tblQLDTTXAChuongTrinhHocChiTietService;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 


    private TblQLDTTXALopMonHocDuKien tblQLDTTXALopMonHocDuKien; 
    public TblQLDTTXALopMonHocDuKien  getTblQLDTTXALopMonHocDuKien() {
        return tblQLDTTXALopMonHocDuKien;
 }

    private TblQLDTTXALopMonHocDuKienService tblQLDTTXALopMonHocDuKienService; 

	private int preInsert(Message msg, TblQLDTTXALopMonHocDuKien obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postInsert(Message msg, TblQLDTTXALopMonHocDuKien obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
	 
    public String execute() throws Exception 
   { 
    try{ 
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	     
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc_DuKien", "them")){ 
	        return "noprivilege"; 
	    } 
	    
        tblQLDTTXALopMonHocDuKienService = (TblQLDTTXALopMonHocDuKienService) context.getBean("tbl_QLDTTXA_LopMonHoc_DuKienClient"); 
        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAChuongTrinhHocChiTietService = (TblQLDTTXAChuongTrinhHocChiTietService) context.getBean("tbl_QLDTTXA_ChuongTrinhHoc_ChiTietClient");
        tblQLDTTXADMKhoaHocService = (TblQLDTTXADMKhoaHocService)  context.getBean("tbl_QLDTTXA_DM_KhoaHocClient"); 


        msg = new Message(); 
        
        //lay ra cac khoa hoc so voi hoc ky hien tai < 10 (5x2) hoc ky
        TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam =tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId));

        int years = tblQLDTTXAHocKyTrongNam.getThoiGianBatDau().getYear() - 5;
        
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        //Get hocVien
        cons.clear();
        if(years > 0){
            ConditionReport con = new ConditionReport(); 
            con.setCol("nam");
            con.setValue1(years);
            con.setOperator("ge");
            cons.add(con);	

        }

        
        List<TblQLDTTXADMKhoaHoc> tblKhoaHocs = tblQLDTTXADMKhoaHocService.findtblQLDTTXADMKhoaHocByCondition(cons, 0, 0);
        
        for(TblQLDTTXADMKhoaHoc tblKhoaHoc : tblKhoaHocs){
	        int yearNo = tblQLDTTXAHocKyTrongNam.getThoiGianBatDau().getYear() - tblKhoaHoc.getNam() ;
	        int hocKyHienTai = 1;
	        
	        if(yearNo ==0)
	        	hocKyHienTai = 1;
	        else if(yearNo > 0){
	        	if(tblQLDTTXAHocKyTrongNam.getThoiGianBatDau().getMonth() >=9)
	        		hocKyHienTai = yearNo*2 + 1;
	        	else 
	        		hocKyHienTai =  yearNo*2;
	        }
	        
	        //lay chuong trinh hoc cho cac khoa hoc
	        cons.clear();
	        if(hocKyHienTai > 0){
	            ConditionReport con = new ConditionReport(); 
	            con.setCol("hocKyId.id");
	            con.setValue1(new Long(hocKyHienTai));
	            con.setOperator("eq");
	            cons.add(con);	

	        }
	        List<TblQLDTTXAChuongTrinhHocChiTiet> tblChuongTrinhChiTiets = tblQLDTTXAChuongTrinhHocChiTietService.findtblQLDTTXAChuongTrinhHocChiTietByCondition(cons, 0, 0);
	        
	        //them vao lop du kien
	        for(TblQLDTTXAChuongTrinhHocChiTiet tblChuongTrinhHocChiTiet : tblChuongTrinhChiTiets ){

	            tblQLDTTXALopMonHocDuKien = new TblQLDTTXALopMonHocDuKien(); 
	            tblQLDTTXALopMonHocDuKien.setMonHocId(tblChuongTrinhHocChiTiet.getMonHocId());
	            tblQLDTTXALopMonHocDuKien.setHocKyId(tblQLDTTXAHocKyTrongNam); 
	            
	            tblQLDTTXALopMonHocDuKien.setDuocMo(false); 
	            
	            //kiem tra lop da duoc mo chua
	            cons.clear();
	            {
		            ConditionReport con = new ConditionReport(); 
		            con.setCol("hocKyId.id");
		            con.setValue1(new Long(hocKyId));
		            con.setOperator("eq");
		            cons.add(con);	
	            }
	            {
		            ConditionReport con = new ConditionReport(); 
		            con.setCol("monHocId.id");
		            con.setValue1(tblChuongTrinhHocChiTiet.getMonHocId().getId());
		            con.setOperator("eq");
		            cons.add(con);	
	            }
	            List<TblQLDTTXALopMonHocDuKien> lopList = tblQLDTTXALopMonHocDuKienService.findtblQLDTTXALopMonHocDuKienByCondition(cons, 0, 0);
	            //neu chua dc mo thi mo lop
	            if(lopList == null || lopList.size() == 0)
	            	tblQLDTTXALopMonHocDuKienService.createtblQLDTTXALopMonHocDuKien(tblQLDTTXALopMonHocDuKien); 
	           
	        }
   
        }
        return Action.SUCCESS;
      } 
      catch(Exception e){ 
         System.out.println("Loi Insert tbl_QLDTTXA_LopMonHoc_DuKien Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
